<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <style>
    .dragging {
      opacity: 0.5;
    }
  </style>

  <div id="draggable" draggable="true">
    Drag me!
  </div>

  <div id="droppable">
    Drop here!
  </div>

  <script type="module">
    document.addEventListener('DOMContentLoaded', function () {
      const draggable = document.getElementById('draggable');
      const droppable = document.getElementById('droppable');

      // 拖动开始时调用
      draggable.addEventListener('dragstart', dragStart);
      // 允许放置
      droppable.addEventListener('dragover', dragOver);
      // 放置后调用
      droppable.addEventListener('drop', drop);

      function dragStart(event) {
        event.dataTransfer.setData('text/plain', this.id); // 设置要传输的数据
        this.classList.add('dragging'); // 添加类以更改样式
      }

      function dragOver(event) {
        event.preventDefault(); // 阻止默认行为
      }

      function drop(event) {
        event.preventDefault();
        const data = event.dataTransfer.getData('text/plain'); // 获取数据
        const draggedElement = document.getElementById(data);
        this.appendChild(draggedElement); // 将拖动的元素移动到放置区
        draggedElement.classList.remove('dragging'); // 移除类
      }
    });

  </script>
</body>

</html>